<template>
    <div class="login-container">
        <div class="login_header">Auth Manage Web</div>
        <div class="login_form">
            <el-form :model="loginForm" :rules="loginFormRules" ref="loginForm" label-width="100px">
                <el-form-item prop="username" label="用户名">
                    <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
                </el-form-item>
                <el-form-item prop="password" label="密码">
                    <el-input v-model="loginForm.password" placeholder="请输入密码"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button @click="submitLoginForm" type="primary" size="small">登录</el-button>
                    <el-button @click="resetForm" size="small">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    name: 'login-index',
    data() {
        return {
            loginForm: {
                username: '',
                password: ''
            },
            loginFormRules: {
                username: [
                    {required: true, message: "请输入用户名", trigger: "blur"},
                    {
                        min: 1,
                        max: 20,
                        message: "长度在 1 到 20 个字符",
                        trigger: "blur",
                    },
                ],
                password: [
                    {required: true, message: "请输入登录密码", trigger: "blur"},
                    {
                        min: 1,
                        max: 20,
                        message: "长度在 1 到 20 个字符",
                        trigger: "blur",
                    },
                ]
            }
        };
    },
    methods: {
        submitLoginForm() {
            const _this = this;
            // 提交登录表单
            _this.$refs.loginForm.validate(valid => {
                if (valid) {
                    // 校验通过
                    const params = {
                        username: _this.loginForm.username,
                        password: _this.loginForm.password
                    };
                    this.$store.dispatch('login', params).then(() => {
                        // 跳转到首页
                        this.$router.push({path: '/'});
                    }).catch(error => {
                        this.$message.warning(error.message+'222');
                    });
                }
            });
        },
        resetForm() {
            this.$refs.loginForm.resetFields();
        }
    }
}
</script>

<style>
.login-container {
    padding: 20px;
    width: 400px;
    margin: 0 auto;
}

.login-container > div {
    width: 100%;
    margin-bottom: 30px;
}

.login-container > div:nth-child(1) {
    text-align: center;
}

.login_header {
    font-size: 18px;
    font-weight: 600;
}
</style>